/***************************************************************************
 *   Copyright (C) 2006 by Archimedes Projekt   *
 *   thelsb@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "ahkpclient.h"
#include <QSettings>
#include <QDataStream>
#include "qhkp.h"

//changes:
//- Now always (bot BYTE and PIN) returing 8 bit data, and in a command structure

AHkpClient::AHkpClient(QHkp *hkp,QObject *parent)
 : QTcpSocket(parent),
 m_hkp(hkp)
{
    //QSettings settings("../../gss.conf",QSettings::IniFormat);
    //QString serialPort = settings.value("Serial_Ports/HPSC/port"/*,"/dev/ttyS0"*/).toString();
    //if(!serialPort.isEmpty())
    //    m_hpsc = new CHPSC(serialPort,this);
    //else std::cout<<"HPSCClient: could not find HPSC serial port setting in config file"<<std::endl;
    connect(this,SIGNAL(readyRead()),this,SLOT(getCommand()));
    //if(m_hkp)
    //connect(this,SIGNAL(sig_writeData(QHkp::port,unsigned char)),m_hkp,SLOT(writeData(QHkp::port,unsigned char)));
    
}


AHkpClient::~AHkpClient()
{
}


/*!
    \fn AHkpClient::setHkp(QHkp *hkp)
 */
void AHkpClient::setHkp(QHkp *hkp)
{
    m_hkp = hkp; 
    if(m_hkp)
        connect(this,SIGNAL(sig_writeData(QHkp::port,unsigned char)),m_hkp,SLOT(writeData(QHkp::port,unsigned char)));
}


/*!
    \fn AHkpClient::getCommand()
 */
void AHkpClient::getCommand()
{
    //std::cout<<"AHkpClient::getCommand()"<<std::endl;
    QDataStream stream(this);
    quint8 data=0;
    HkpCommand command;
    while(bytesAvailable() >= sizeof(HkpCommand))
    {
        stream>>command;
        
        if(command.type == READ_IN)
        {
            switch(command.mode)
            {
                case PIN:
                    if(m_hkp->pin(command.port,true,command.pin))
                    {
                    //command.data = 1 << command.pin;
                    command.data = command.pin;
                    //std::cout<<"read in pin"<<command.pin<<" ON"<<std::endl;
                    }
                    //else std::cout<<"read pin"<<command.pin<<" OFF"<<std::endl;
                    stream<<command;
                    this->flush();
                    break;
                case BYTE:
                    m_hkp->readData(command.port,&data);
                    //std::cout<<"read in data:"<<(int)data<<std::endl;
                    command.data = data;
                    stream<<command;
                    this->flush();                        
                    break;
                default: break;    
            }
        }
        
        if(command.type == READ_OUT)
        {
            switch(command.mode)
            {
                case PIN:
                    //std::cout<<"READ_OUT: pin"<<std::endl;
                    if(m_hkp->pin(command.port,false,command.pin))
                    command.data = command.pin; 
                    stream<<command;
                    this->flush();
                    break;
                case BYTE:
                    data = m_hkp->getPins(command.port);
                    //std::cout<<"read out data:"<<(int)data<<" port:"<<command.port<<std::endl;
                    command.data = data;
                    stream<<command;
                    this->flush();  
                    break;
                default: break;
            }
        }
        
        if(command.type == WRITE)
        {
            switch(command.mode)
            {
                case PIN_ON:
                    std::cout<<"PIN_ON:pin"<<command.pin<<" port:"<<(int)command.port<<std::endl;
                    m_hkp->setOn(command.port,command.pin);
                    //std::cout<<"after seton"<<std::endl;
                break;
                case PIN_OFF: 
                    std::cout<<"PIN_OFF:pin"<<command.pin<<" port:"<<(int)command.port<<std::endl;
                    m_hkp->setOff(command.port,command.pin);
                    //std::cout<<"after setoff"<<std::endl;
                break;
                default: break;
            }
            
        }
    }
}
